import 'package:get/get.dart';
import 'package:native_dart_package/http/request/exception/exception_handler.dart';
import 'package:native_dart_package/loading/loading_util.dart';
import 'package:yt_devops/components/paging/model/paging_params.dart';
import 'package:yt_devops/pages/devices/device_list/device_list_controller.dart';
import 'package:yt_devops/pages/site/site_device_list/site_device_list_controller.dart';
import 'package:yt_devops/services/common/common_services.dart';

import '../../../../components/log_utils.dart';
import '../../../../resources/str_res_keys.dart';
import '../../../../routers/router_path.dart';
import '../../../../services/device/device_sevices.dart';
import '../access_device/model/access_device_model.dart';
import '../access_device/model/device_type_detail_model.dart';
import '../access_device/model/device_type_model.dart';
import '../access_device/views/infoHeaderView.dart';

class AccessDeviceThreeController extends GetxController {
  AccessProgressType type = AccessProgressType.completed;
  final DeviceServices services = Get.put(DeviceServices());
  final CommonServices uploadServices = Get.put(CommonServices());
  AccessDeviceModel unitMode = AccessDeviceModel();
  DeviceTypeModel typeModel = DeviceTypeModel();
  DeviceTypeDetailModel typeDetailModel = DeviceTypeDetailModel();
  DeviceListController controller = Get.find(tag: RouterPath.deviceList);
  String deviceName = '';
  String deviceSn = '';
  var arguments;
  List<String> titleList = [];
  List<String> detailList = [];

  ///设备图片列表
  List<String> deviceImageList = [];

  @override
  void onInit() {
    super.onInit();
    arguments = Get.arguments;
    Log.d('==arguments==' + arguments.toString());
    if (arguments != null) {
      if (arguments['imageList'] != null) {
        deviceImageList = arguments['imageList'];
      }
      unitMode = arguments['unitName'];
      typeModel = arguments['productType'];
      typeDetailModel = arguments['productModel'];
      deviceName = arguments['deviceName'];
      deviceSn = arguments['deviceSn'];
      titleList = [
        // ResKey.siteDeviceAccessOwningUnit.tr,
        ResKey.siteDeviceAccessProductType.tr,
        ResKey.siteDeviceAccessProductModel.tr,
        ResKey.deviceName.tr,
        (arguments['paramConfigType'] != null &&
                arguments['paramConfigType'].toString() == '6')
            ? ResKey.siteDeviceAccessEMSSNNumber.tr
            : ResKey.deviceSn.tr,
      ];
      detailList = [
        // unitMode.name ?? '',
        typeModel.name ?? '',
        typeDetailModel.model ?? '',
        deviceName ?? '',
        deviceSn ?? '',
      ];
    }
  }

  @override
  void onReady() {}

  @override
  void onClose() {}

  Future<void> complete() async {
    var param = {
      // 'siteId': arguments['siteId'] ?? 1,
      'productId': typeDetailModel.id ?? '',
      'productType': typeModel.id ?? '',
      'model': typeDetailModel.model ?? '',
      'name': deviceName ?? '',
      'sn': deviceSn ?? '',
      'associateIds': arguments['associateId'],
      'config': arguments['config']
    };

    if (deviceImageList.isNotEmpty) {
      param['photos'] = deviceImageList.join(',');
    }
    Log.d('param==$param');
    // _goBackToDeviceListPage();
    // return;
    showLoading(toast: ResKey.loading.tr);
    var result = await services.postDeviceAccess(param, onError: (error) {
      handleException(error);
      dismissLoading();
      return false;
    });
    dismissLoading();
    if (result != null) {
      _goBackToDeviceListPage();
    }
  }

  ///上传配置信息
  postConfig(deviceId) async {
    ///paramConfigType 我们只需要关心1,2,4,6  1储能 2电表,4是逆变器的,6输入emsSn
    if (arguments['paramConfigType'] != null) {
      if (arguments['paramConfigType'] == 1) {
        var param = {
          'deviceId': deviceId,
          'name': deviceName ?? '',
          'productId': typeDetailModel.id ?? '',
          'paramConfigType': arguments['paramConfigType'],
          'config': arguments['config']
        };

        Log.d('postConfig==' + param.toString());

        var response = await services.meterConfigs(param, onError: (error) {
          handleException(error);
          return false;
        });
        if (response != null) {
          _goBackToDeviceListPage();
        }
      } else if (arguments['paramConfigType'] == 2) {
        var param = {
          'deviceId': deviceId,
          'name': deviceName ?? '',
          'productId': typeDetailModel.id ?? '',
          'paramConfigType': arguments['paramConfigType'],
          'config': arguments['config']
        };
        var response = await services.meterConfigs(param, onError: (error) {
          handleException(error);
          return false;
        });
        var param1 = {
          'deviceId': deviceId,
          'name': deviceName ?? '',
          'productId': typeDetailModel.id ?? '',
          'productConfigType': arguments['productConfigType'],
          'associateIds': arguments['associateId']
        };
        var response1 =
            await services.configsRelationDevice(param1, onError: (error) {
          handleException(error);
          return false;
        });
        if (response != null && response1 != null) {
          _goBackToDeviceListPage();
        }
      } else if (arguments['paramConfigType'] == 4) {
        var param = {
          'deviceId': deviceId,
          'name': deviceName ?? '',
          'productId': typeDetailModel.id ?? '',
          'paramConfigType': arguments['paramConfigType'],
          'config': arguments['config']
        };
        var response = await services.meterConfigs(param, onError: (error) {
          handleException(error);
          return false;
        });
        if (response != null) {
          _goBackToDeviceListPage();
        }
        Log.d('response=' + response.toString());
      }
    }
  }

  ///返回到设备列表页
  _goBackToDeviceListPage() async {
    controller.refreshData();
    Get.until((route) => route.settings.name == RouterPath.productManageHome);
  }
}
